package com.road.vast.springboot;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.runtime.ProcessInstanceQuery;

import java.util.List;

/**
 * @Author 郭泽鹏
 * @Date 2020/6/26 16:53
 * @Version 1.0
 * @Description 单个流程实例的挂起与激活
 */
public class SuspendProcessInstanceSingle {
    public static void main(String[] args) {
        //创建ProcessEngine对象
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();

        //创建RuntimeService对象
        RuntimeService runtimeService = defaultProcessEngine.getRuntimeService();

        //创建ProcessInstanceQuery查询器
        ProcessInstanceQuery processInstanceQuery = runtimeService.createProcessInstanceQuery();

        //设置查询条件
        String processInstanceId = "30001";
        processInstanceQuery.processInstanceId(processInstanceId);

        //查询结果
        List<ProcessInstance> list = processInstanceQuery.list();

        //遍历查询结果
        for (ProcessInstance processInstance : list) {
            boolean suspended = processInstance.isSuspended();

            if (suspended){
                runtimeService.activateProcessInstanceById(processInstanceId);
                System.out.println("流程实例ID号：" + processInstanceId + "已激活");
            }else{
                runtimeService.suspendProcessInstanceById(processInstanceId);
                System.out.println("流程实例ID号：" + processInstanceId + "已挂起");
            }
        }
    }
}
